#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry
import sys
import os
#import locale
#import json
#import subprocess
#from functools import partial
from imp import reload
#import jarryLibs
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class ScriptListWidget(QListWidget):
    '''
        action icon list widget
    '''

    def __init__(self):
        super(ScriptListWidget, self).__init__()
        self.style_setup()
        self.setup_items()

        self.itemClicked.connect(self.db_run)
        self.doubleClicked.connect(self.db_run)
        self.setMouseTracking(True)

    def style_setup(self):
        # self.setFixedWidth(400)
        self.setStyleSheet(""
                           "QListWidget::Item:hover{color:rgba(255, 255, 255, 255);background:rgb(82, 133, 166); }"
                           "")
        self.setSpacing(14)
        # self.setIconSize(QSize(60, 60))
        self.setViewMode(QListView.IconMode)
        self.setResizeMode(QListWidget.Adjust)
        self.setWordWrap(True)
        self.setWrapping(True)
        self.setMovement(QListView.Static)
        # self.resizeRowsToContents(True)
        # self.setGridSize(QSize(80, 70))
        # self.setSortingEnabled(True)
        # self.setContextMenuPolicy(Qt.CustomContextMenu)
        # self.customContextMenuRequested.connect(self.showContextMenu)

    def setup_items(self):
        self.clear()

    def db_run(self):
        item = self.currentItem()
        command = item.command
        import maya.cmds as cmds
        exec(command)

    def setupItemIcon(self, item, size, iconPath):
        tile_size = size * 1

        tile = QImage(tile_size, tile_size, QImage.Format_ARGB32)
        painter = QPainter()
        painter.begin(tile)
        target_rect = QRect(0.0, 0.0, tile_size, tile_size)
        pixmap = QPixmap(iconPath)
        painter.drawPixmap(target_rect, pixmap)

        painter.end()

        tile_pixmap = QPixmap.fromImage(tile)

        item.setIcon(tile_pixmap)


if __name__ == '__main__':
    app = QApplication([])
    my_ui = LauncherListWidget()
    # my_ui = EnvUI()
    my_ui.show()
    app.exec_()
